package com.projectsexception.festivales.setup.xml;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.res.XmlResourceParser;

import com.projectsexception.festivales.setup.BandInfo;
import com.projectsexception.festivales.setup.Channel;
import com.projectsexception.festivales.setup.OfficialChannelInfo;
import com.projectsexception.festivales.setup.ShowInfo;
import com.projectsexception.festivales.setup.StageInfo;
import com.projectsexception.festivales.util.DateFormater;

public class XMLHandlerShowInfo extends AbstractXMLHandler<ShowInfo> {
    
    private List<ShowInfo> shows;
    private Map<String, BandInfo> bands;
    private Map<String, StageInfo> stages;
    private BandInfo tmpBandInfo;
    private boolean bandBiography;

    @Override
    public void startDocument() {
        shows = new ArrayList<ShowInfo>();
        bandBiography = false;
    }

    @Override
    public void startTag(XmlResourceParser res) {
        String s = res.getName();
        if (s.equals("bands")) {
            bands = new HashMap<String, BandInfo>();
        } else if (s.equals("band")) {
            tmpBandInfo = new BandInfo();            
            tmpBandInfo.setName(res.getAttributeValue(null, "name"));
            tmpBandInfo.setPhotoId(res.getAttributeResourceValue(null, "photoId", 0));
            bands.put(res.getAttributeValue(null, "id"), tmpBandInfo);
        } else if (s.equals("biography")) {
            bandBiography = true;
        } else if (s.equals("officialChannels")) {
            tmpBandInfo.setOfficialChannels(new ArrayList<OfficialChannelInfo>());
        } else if (s.equals("channel")) {
            OfficialChannelInfo channel = new OfficialChannelInfo();
            channel.setType(Channel.valueOf(Channel.class, res.getAttributeValue(null, "type")));
            channel.setLink(res.getAttributeValue(null, "link"));
            tmpBandInfo.getOfficialChannels().add(channel);
        } else if (s.equals("stages")) {
            stages = new HashMap<String, StageInfo>();
        } else if (s.equals("stage")) {
            StageInfo stageInfo = new StageInfo();
            stageInfo.setName(res.getAttributeValue(null, "name"));
            stages.put(res.getAttributeValue(null, "id"), stageInfo);
        } else if (s.equals("show")) {
            ShowInfo showInfo = new ShowInfo();
            showInfo.setBand(bands.get(res.getAttributeValue(null, "band")));
            showInfo.setStage(stages.get(res.getAttributeValue(null, "stage")));
            showInfo.setDate(DateFormater.parseDate(res.getAttributeValue(null, "date")));
            showInfo.setStartTime(res.getAttributeValue(null, "startTime"));
            showInfo.setEndTime(res.getAttributeValue(null, "endTime"));
            shows.add(showInfo);
        }
    }
    
    @Override
    public void text(XmlResourceParser res) {
        if (bandBiography) {
            tmpBandInfo.setBiography(res.getText().trim());
        }
    }
    
    @Override
    public void endTag(XmlResourceParser res) {
        String s = res.getName();
        if (s.equals("biography")) {
            bandBiography = false;
        }
    }

    @Override
    public List<ShowInfo> getResult() {
        return shows;
    }

}
